package 蓝桥算法;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class day05 {//有效的字母异位
    public boolean isAnagram(String a,String b){
        int[] record=new int[26];
        for (int i = 0; i <a.length() ; i++) {
            record[(a.charAt(i)-'a')]++;
        }
        for (int i = 0; i < b.length(); i++) {
            record[(b.charAt(i)-'a')]--;//规定相应的索引
        }
        for (int i = 0; i < record.length; i++) {
            if(record[i]!=0){
                return  false;
            }
        }
        return true;
    }

    public int[] intersection(int[] num1,int[] num2){
        if(num1==null||num1.length==0){
            return new int[0];
        }
        Set<Integer> set1= new HashSet<>();
        Set<Integer> resSet=new HashSet<>();
        for(int i:num1){
            set1.add(i);
        }
        for(int i:num2){//遍历数组num2
            if(set1.contains(i)){
                resSet.add(i);
            }
        }
        return resSet.stream().mapToInt(x->x).toArray();//把集合转化为数组
    }
    public int[] intersection2(int[] nums1,int[] nums2){
        int[] hash1=new int[1001];
        int[] hash2=new int[1002];
        for(int i=0;i<nums1.length;i++){
            hash1[nums1[i]]++;
        }
        for(int i=0;i<nums2.length;i++){
            hash2[nums2[i]]++;
        }
        List<Integer> resList=new ArrayList<>();
        for(int i=0;i<1001;i++){
            if((hash1[i]!=0)&&(hash2[i]!=0)){
                resList.add(i);
            }
        }
        int[] res=new int[resList.size()];
        for(int i=0;i<res.length;i++){
            res[i]=resList.get(i);
        }
        return res;
    }

    public static void main(String[] args) {
        day05 x=new day05();
        System.out.println(x.isAnagram("adesf","agdhf"));
        System.out.println( x.isAnagram("adsf","asdf"));
        int[] a={12,3,45,3,4,3};
        int [] b={12,3,2,4,2,4};
        int []y=x.intersection(a,b);
        for(int i:y){
            System.out.println(i);
        }
        int[] z=x.intersection2(a,b);
        for(int i:z){
            System.out.println(i);
        }
    }
}
